package com.runner.track;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.runner.entity.CollisionEntity;
import com.runner.utility.Locator;

public class Ground extends CollisionEntity {

	public static float SCALE = 1;
	public static final float SPEED = 220 * RELATIVE_SPEED;
	private Texture background, background2;
	private float position, position2;
	private SpriteBatch uiBatch;
	private Stage stage;

	public Ground() {
		super(new Vector2(0, 0), TYPE.GROUND, NULL_GROUND_PATH, true, false, false, SCALE);
		background = Locator.getAssets().get(GROUND_PATH, Texture.class);
		background2 = Locator.getAssets().get(GROUND_PATH, Texture.class);
		position = 0;
		position2 = ACTUAL_VIRTUAL_WIDTH;
		uiBatch = new SpriteBatch();
		stage =new Stage(Locator.getViewport());
		uiBatch.setTransformMatrix(stage.getCamera().view);
		uiBatch.setProjectionMatrix(stage.getCamera().projection);
	}

	public void render(SpriteBatch sb) {
		super.render(sb);
		sb.end();
		uiBatch.begin();
		uiBatch.draw(background2, position2, 0, 0, 0, background.getWidth(), background.getHeight(), Locator.getxSpriteScale(),
				Locator.getySpriteScale(), 0, 0, 0, (int) background.getWidth(), (int) background.getHeight(), false, false);
		uiBatch.draw(background, position, 0, 0, 0, background.getWidth(), background.getHeight(), Locator.getxSpriteScale(),
				Locator.getySpriteScale(), 0, 0, 0, (int) background.getWidth(), (int) background.getHeight(), false, false);
		uiBatch.end();
		sb.begin();
	}

	/**
	 * Move 2 different grounds at the same time, putting position of one
	 * offscreen as the next one on the right side
	 */
	public void update(float delta) {
		super.update(delta);
		position -= SPEED * Locator.getXGameSpeed() * delta;
		position2 -= SPEED * Locator.getXGameSpeed() * delta;
		if (position <= -ACTUAL_VIRTUAL_WIDTH) {
			position = ACTUAL_VIRTUAL_WIDTH;
		}
		if (position2 <= -ACTUAL_VIRTUAL_WIDTH) {
			position2 = ACTUAL_VIRTUAL_WIDTH;
		}
	}

	@Override
	public void collide(CollisionEntity otherBody) {
	}

}
